package fr.fermat.lyricsext;

import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		        Intent intent = getIntent();

		        // Retrieve the URI from the intent, this is a URI to a MediaStore audio
		        // file
		        Uri trackUri = intent.getData();

		        // Use it to query the media provider
		        Cursor trackCursor = getContentResolver().query(
		                trackUri,
		                new String[] {
		                        MediaStore.Audio.Media.TITLE, MediaStore.Audio.Media.ARTIST,
		                        MediaStore.Audio.Media.ALBUM
		                }, null, null, null);

		        if (trackCursor != null) {
		            try {
		                if (trackCursor.moveToFirst()) {

		                    // And retrieve the wanted information
		                    String trackName = trackCursor.getString(trackCursor
		                            .getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE));
		                    String albumName = trackCursor.getString(trackCursor
		                            .getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM));
		                    String artistName = trackCursor.getString(trackCursor
		                            .getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST));

		                    ((TextView)findViewById(R.id.track)).setText(trackName);
		                    ((TextView)findViewById(R.id.album)).setText(albumName);
		                    ((TextView)findViewById(R.id.artist)).setText(artistName);

		                }
		            } finally {
		                trackCursor.close();
		            }
		        }

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}
